Dim cHFMwSession
Dim oMngApps
Dim cHsvSession
Dim cHsvWebClient
Dim cHsvResourceManager
Dim nLanguageID


Function openHfmApp(sDomain, sUser, sPass, sServer, sApp)
	Dim cClient, cSession, cServer
	Set cClient = CreateObject ("Hyperion.HsxClient.1")
	cClient.SetLogonInfoSSO sDomain, sUser, "", sPass
	cClient.OpenApplication sServer, "Financial Management", App, cServer, cSession
	Set openHfmApp = cSession
End Function


Sub InitSession(sDomain, sServer, sApp, sUserName, sSSOToken, sPassword )
	On Error GoTo 0
	Set oMngApps = CreateObject("Hyperion.HFMwManageApplications")
		
	If Trim(sDomain) <> "" And Trim(sApp) <> "" And ((Trim(sUserName) <> "" AND Trim(sPassword) <> "") OR (Trim(sSSOToken) <> "" )) Then
		Set cHFMwSession = nothing
		oMngApps.SetLogonInfoSSO sDomain, sUserName, sSSOToken, sPassword 
		Set cHFMwSession = oMngApps.OpenApplication(Trim(sDomain), Trim(sApp))
	End If
	
	If IsObject(cHFMwSession) Then
		Set cHsvSession = cHFMwSession.session
		
		Set cHsvWebClient = CreateObject("Hyperion.HsvWebClient")
		cHsvWebClient.SetSession(cHsvSession)
	
	End If

	Exit Sub
End Sub

Function GetMemberID(lDimID  , sMemLabel)
	Dim cTreeInfo
	'g_cMetadata is an HsvMetadata object reference.
	set cMetadata = cHFMwSession.Metadata
	Set cTreeInfo = cMetadata.Dimension(lDimID)
	GetMemberID = cTreeInfo.GetItemID(sMemLabel)
End Function



Function GetItemID (lDimID  , sMemLabel)
	GetItemID = cHsvWebClient.GetItemID(lDimID, sMemLabel)
End Function

Function Consolidate (Scenario, Year, Period, Parent, Entity)
	Dim lScen, lYear, lPer  
	Dim lEnt, lPar  
	Dim cCalculate
	lScen = GetItemID(0, Scenario)
	lYear = GetItemID(1, Year)
	lPer = GetItemID(2, Period)
	lEnt = GetItemID(4, Entity)
	lPar = GetItemID(4, "")
	'lPar = 0
	Set cCalculate = cHsvSession.Calculate
	'msgbox lScen&","&lYear&","&lPer&","&lEnt&","&lPar
	cCalculate.Consolidate lScen, lYear, lPer, lEnt, lPar, 2
End Function

'InitSession "HYPERION02", "", "consol0406", "admin", "", "password"
'Consolidate "Actual", "2014", "FM11", "GLPGroup", "CN_SEG"
'msgbox WScript.Arguments(1)&","&WScript.Arguments(2)&","&WScript.Arguments(3)&","&WScript.Arguments(4)&","&WScript.Arguments(5)&","&WScript.Arguments(6)&","&WScript.Arguments(7)&","&","&WScript.Arguments(8)
InitSession WScript.Arguments(1), "", WScript.Arguments(2), WScript.Arguments(3), "", WScript.Arguments(4)
Consolidate WScript.Arguments(5), WScript.Arguments(6), WScript.Arguments(7), "", WScript.Arguments(8)